﻿@{
}
@model DataAccess.Models.Role

<script src="~/Scripts/plugins/jquery.min.js"></script>
<script src="~/Scripts/plugins/layer/layer.min.js"></script>
@section Styles{
    <link href="//cdn.bootcss.com/bootstrap-treeview/1.2.0/bootstrap-treeview.min.css" rel="stylesheet" />
}
@section Scripts{
    <script src="http://cdn.bootcss.com/bootstrap-treeview/1.2.0/bootstrap-treeview.min.js"></script>

    <script type="text/javascript">
        var index = parent.layer.getFrameIndex(window.name);
       
        $(function () {
            $("#cancel").click(function () {
                parent.location.reload();
                parent.layer.close(index);
            });
           // $(".chosen-select").chosen();

            initTree();

            $("#submit").click(function () {
                var arr = $('#tree').treeview('getChecked', 0);
                var arrIds = new Array();
                $(arr).each(function (i,e) {
                    if (e.state.checked)
                    {
                        arrIds.push(e.bizId);
                    }
                });

                var arrStr = arrIds.join(',');
               
                common.invokeApi(
              "post",
              "/setRole.html",
              {
                  id: $("#id").val(), ids: arrIds.join(',')
              },
              function () { },
              function (data) {
                  common.msg(data.Message, 1, function () {
                     
                  });
              },
              function (data) {
                  common.msg(data.Message, 2);
              },
              function () { }
              );
            });
             
        });

        function initTree() {

            //$('#tree').treeview({ data: getTree() });
            common.invokeApi(
                           "get",
                           "/getPermissions.html?roleid=@Model.id",
                           null,
                           function () { },
                           function (data) {
                              
                               $(data.Datas).each(function (i, e) {
                                   CheckedState(e);
                               });




                               $('#tree').treeview({
                                   data: data.Datas,
                                   showIcon: false,
                                   showCheckbox: true,
                                   showIcon:false,
                                   onNodeChecked: function (event, node) {
                                       selectedAllParentNode(node)
                                       //$('#checkable-output').prepend('<p>' + node.text + ' was checked</p>');
                                       var selectNodes = getNodeIdArr(node);//获取所有子节点
                                       if (selectNodes) { //子节点不为空，则选中所有子节点
                                           $('#tree').treeview('checkNode', [selectNodes, { silent: true }]);
                                       }
                                   },
                                   onNodeUnchecked: function (event, node) {
                                       unSelectedAllParentNode(node)
                                       //$('#checkable-output').prepend('<p>' + node.text + ' was unchecked</p>');
                                       var selectNodes = getNodeIdArr(node);//获取所有子节点
                                       if (selectNodes) { //子节点不为空，则取消选中所有子节点
                                           $('#tree').treeview('uncheckNode', [selectNodes, { silent: true }]);
                                       }
                                   } 
                               });
                           },
                           function (data) { 
                           },
                           function () { }
                           );
        }
        function getNodeIdArr(node) {
            var ts = [];
            if (node.nodes) {
                for (x in node.nodes) {
                    ts.push(node.nodes[x].nodeId)
                    if (node.nodes[x].nodes) {
                        var getNodeDieDai = getNodeIdArr(node.nodes[x]);
                        for (j in getNodeDieDai) {
                            ts.push(getNodeDieDai[j]);
                        }
                    }
                }
            } else {
                ts.push(node.nodeId);
            }
            return ts;
        }
        function selectedAllParentNode(node) {

            var parentNode = $('#tree').treeview('getParent', node);

            if (parentNode && parentNode.state && !parentNode.state.checked) {
                $('#tree').treeview('toggleNodeChecked', [parentNode, { silent: true }]);
            }

            if (parentNode && parentNode.state) {
                selectedAllParentNode(parentNode);
            }
        }
        function unSelectedAllParentNode(node) {
            var siblingNodes = $('#tree').treeview('getSiblings', node);

            var select = false;

            if (siblingNodes) {
                $(siblingNodes).each(function (i, e) {
                    if (e.state && e.state.checked) {
                        select = true;
                    }
                }); 
            } 
            if (!select) {
                var parentNode = $('#tree').treeview('getParent', node);
                if (parentNode && parentNode.state && parentNode.state.checked) {
                    $('#tree').treeview('toggleNodeChecked', [parentNode, { silent: true }]);
                }
                if (parentNode && parentNode.state) {
                    unSelectedAllParentNode(parentNode);
                }
            }
        }
      
        function CheckedState(node)
        {
            node.state.checked = node.state.Checked;
            if(node.nodes)
            {
                $(node.nodes).each(function (i, e) {
                    CheckedState(e);
                });
            }
        }
         
    </script>

    <script src="~/Scripts/plugins/bootstrap.min.js"></script>
    <script src="~/Scripts/plugins/content.min.js"></script>
    <script src="~/Scripts/sys/lib/common_raw.js"></script>
}
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-sm-12">

        </div>
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-content">
                    <form method="post" class="form-horizontal" id="form1">
                        <input type="hidden" id="id" name="id" value="@(Model.id)" />
                        <div class="form-group">
                            <div id="tree"></div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group" style="text-align:center;">
                            <button class="btn btn-primary" id="submit" type="button">保存</button>
                            <button class="btn btn-white" id="cancel" type="button" style="margin-left:70px;">关闭</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>